package lea87crzz.llega.el.tren;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AnalogClock;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import ferrovias.clases.*;

public class ResultadoActivity extends ListActivity {
	
	int estOr;
	int estDest;
	int diasint;
	int h1;
	int h2;
	private MyApp appState;
	public void onCreate( Bundle savedInstance ){
        super.onCreate(savedInstance);
        appState=(MyApp)getApplication();
        //this.setContentView(R.layout.resultados);
        String origen=this.getIntent().getExtras().getString("origen");
        String destino=this.getIntent().getExtras().getString("destino");
        h1=Integer.parseInt(this.getIntent().getExtras().getString("h1"));
        h2=Integer.parseInt(this.getIntent().getExtras().getString("h2"));
        String dias=this.getIntent().getExtras().getString("dias");
        estOr=parseName(origen);        
        estDest=parseName(destino);
        diasint=parseDias(dias); 
        loadData();       
        
   }
	private ProgressDialog progressDialog;
	static LinkedList<TrenBelgranoNorte> data;
	
	
	private final Handler progressHandler = new Handler() {
    @SuppressWarnings("unchecked")
    public void handleMessage(Message msg) {        
         setData();
         progressDialog.dismiss();
         }
  };

	private void loadData() {
    progressDialog = ProgressDialog.show(this, "", "Calculando trenes...", true);
    new Thread(new Runnable(){
        @Override
        public void run() {
        	Ferrovias tren=new Ferrovias();
            ArrayList<TrenBelgranoNorte> lista=tren.buscarTren(estOr, estDest, new Hora(h1,0), new Hora(h2,0),diasint);
            LinkedList<TrenBelgranoNorte> data1=new LinkedList<TrenBelgranoNorte>();
            HashMap<String, String> mapa;
        	for(TrenBelgranoNorte t:lista){   
        		data1.add(t);
        	} 
        	appState.setData(data1);
            progressHandler.sendEmptyMessage(0);
        	
        	//setData(data1);
        }}).start();
}
	
	static final String DATA_TITLE = "Salida";
	static final String DATA_LINK  = "Llegada";
	
	private void setData(){	
		this.setListAdapter(new MiAdaptador(getApplicationContext(), R.layout.fila, 0, appState.getData()));
    
	}

	private int parseDias(String dias) {
		if(dias!=null)
		{
		if(dias.equals("Lun a Vie")) return 0;
        if(dias.equals("Sabados")) return 1;
        if(dias.equals("Dom y feriados")) return 2;
		}
        return 0;
	}

	private int parseName(String s) {
			if(s.equals("Retiro")) return 0;
			if(s.equals("Saldias")) return 1;
			if(s.equals("Ortiz")) return 2;
			if(s.equals("Aristobulo del valle")) return 3;
	        if(s.equals("Padilla")) return 4;
	        if(s.equals("Florida")) return 5;
	        if(s.equals("Munro")) return 6;
	        if(s.equals("Carapachay")) return 7;
	        if(s.equals("Villa Adelina")) return 8;
	       if(s.equals("Boulogne")) return 9;
	       if(s.equals("Montes")) return 10;
	       if(s.equals("Don Torcuato")) return 11;
	       if(s.equals("Sourdeaux")) return 12;
	       if(s.equals("Villa de Mayo")) return 13;
	       if(s.equals("Los Polvorines")) return 14;
	       if(s.equals("Pablo Nogues")) return 15;
	       if(s.equals("Grand Bourg")) return 16;
	       if(s.equals("Tirras Altas")) return 17;
	       if(s.equals("Tortuguitas")) return 18;
	       if(s.equals("Alberti")) return 19;
	       if(s.equals("Del Viso")) return 20;
	       if(s.equals("Villa Rosa")) return 21;
	       return 0;
	}
}
